****************************************************************************************************************************************************
***Analysis of Experiment 1 in Pedersen, Rasmus T: "Attitudes on the Size of Unemployment Benefits"*************************************************
****************************************************************************************************************************************************
***Written for Stata 15*****************************************************************************************************************************
****************************************************************************************************************************************************

**********************************
***Installing required packages***
**********************************

*Install these packages, if the are not already installed:
*ssc install estout //Run this line if estout is not already installed (used when creating regression tables)
*net install grc1leg2.pkg //Run this line if grc1leg is not already installed (used when combining graphs)
*ssc install coefplot //Run this line if "Coefplot" is not already installed (used when plotting results in graphs)
*ssc install blindschemes //Run this line if "blindschemes is not already installed (used to control graph layout) 

*******************************
***Setting the figure scheme***
*******************************
set scheme plotplainblind

***************************************************************************************************************************************************
***Getting the data********************************************************************************************************************************
***************************************************************************************************************************************************
	clear all
	set more off

	cd  "C:\Attitudes_on_Benefits"
	use "C:\Attitudes_on_Benefits\Benefits_study1.dta" 

***************************************************************************************************************************************************
***Attaching labels*** ****************************************************************************************************************************
***************************************************************************************************************************************************
	label variable 	RecordNo "RecordNo"
	label variable 	q1  "Self placement on LR"
	label variable 	q2  "is a maximum rate of unemployment benefits on 4,135 DKK per week too high, too low or fair"
	label variable 	q3  "is a maximum rate of unemployment benefits on 17,918 DKK per month too high, too low or fair"
	label variable 	q4  "is a maximum rate of unemployment benefits on 6,923 DKK per week too high, too low or fair"
	label variable 	q5  "is a maximum rate of unemployment benefits on 7,000 DKK per week too high, too low or fair"
	label variable 	q6  "is a maximum rate of unemployment benefits on 30,000 DKK per month too high, too low or fair"
	label variable 	q7  "You answered that 4,135 DKK per week is not fair. What do you think that the maximum rate of unemployment benefits should be"
	label variable 	q8  "You answered that 17,918 DKK per month is not fair. What do you think that the maximum rate of unemployment benefits should be"
	label variable 	q9  "You answered that 6,923 DKK per week is not fair. What do you think that the maximum rate of unemployment benefits should be"
	label variable 	q10 "You answered that 7,000 DKK per week is not fair. What do you think that the maximum rate of unemployment benefits should be"
	label variable 	q11 "You answered that 30,000 DKK per month is not fair. What do you think that the maximum rate of unemployment benefits should be"
	label variable 	gender "Gender"
	label variable 	postcode "Zip Code"
	label variable 	profile_education "Education?"
	label variable 	occupation "Main Occupation"
	label variable 	FT_next "Vote in next election"
	label variable 	FT15 "Vote in previous electin"
	label variable 	age "Age"
	label variable 	region "Region"
	label variable 	weight "Weight"
	label variable 	starttime "starttime"
	label variable 	endtime "endtime"
	label variable 	page_pQ1_timing "Timing Data for Page pQ1"
	label variable 	page_q2_timing "Timing Data for Page q2"
	label variable 	page_q3_timing "Timing Data for Page q3"
	label variable 	page_q4_timing "Timing Data for Page q4"
	label variable 	page_q5_timing "Timing Data for Page q5"
	label variable 	page_q6_timing "Timing Data for Page q6"
	label variable 	page_q7_timing "Timing Data for Page q7"
	label variable 	page_q8_timing "Timing Data for Page q8"
	label variable 	page_q9_timing "Timing Data for Page q9"
	label variable 	page_q10_timing "Timing Data for Page q10"
	label variable 	page_q11_timing "Timing Data for Page q11"
	label variable 	page_q12_timing "Timing Data for Page q12"
	label variable 	page_q13_timing "Timing Data for Page q13"
	label variable 	tot_time "Total time"


**********************************************************************************************************************************************************
***Recoding, Creating variables and Attaching Value Labels************************************************************************************************
**********************************************************************************************************************************************************

***Destringing variables***
	foreach var of varlist q2 q3 q4 q5 q6 x y q12_1 q12_2 q12_3 q12_4 q13 gender profile_education occupation FT_next FT15 region {
	encode `var', gen(temp`var')
	drop `var'
	clonevar `var'=temp`var'
	drop temp`var'
	}

	order RecordNo q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 q11 x y q12 q12_1 q12_2 q12_3	q12_4 q13 gender postcode profile_education occupation FT_next FT15 age region weight starttime endtime ///
      page_pQ1_timing page_q2_timing page_q3_timing page_q4_timing page_q5_timing page_q6_timing page_q7_timing page_q8_timing page_q9_timing page_q10_timing page_q11_timing page_q12_timing page_q13_timing tot_time

***Dropping Variables***	  
	drop x y q12 q12_1 q12_2 q12_3 q12_4 q13 //These variables are from a different, subsequent experiment

***Recoding Covariates***

*Left-Right Position*
	recode q1 (997=.), gen(LR_unstd)
	gen LR=LR_unstd/10

	by FT15, sort: egen FT15_median = median(LR)
	by FT_next, sort: egen FT_next_median = median(LR)
	alpha LR FT15_median FT_next_median, item gen(LR_total) 
	gen LR_total_unstd=round(LR_total*10)
	
	gen LR_impute=LR_unstd
	replace LR_impute=LR_total_unstd if LR==.
	recode LR_impute (0 1 2 3=0 "Left") (4 5 6=1 "Center") (7 8 9 10=2 "Right"), gen(LCR)
	
*Gender*
	recode gender (1=1 "female") (2=0 "male"), gen(female)	 

*Education - completetion of tertiary education is set to "some college"*
recode profile_education (1 2 3 5 9=0) (4 6 7 8=1), gen(Somecollege)	
 
	  
***Recoding the IV***
	gen condition=.
	recode condition (missing=1) if q2!=.
	recode condition (missing=2) if q3!=.
	recode condition (missing=3) if q4!=.
	recode condition (missing=4) if q5!=.
	recode condition (missing=5) if q6!=.
	label define condition 1 "Actual Weekly" ///
						   2 "Actual Monthly" ///
						   3 "Suggested Weekly, precise" ///
						   4 "Suggested Weekly, imprecise" ///
						   5 "Suggested Monthly"
	label values condition condition
						   
	recode condition (1 2=0 "Low Anchor") (3 4 5=1 "High Anchor"), gen(High_anchor)
	recode condition (2 5=0 "Montly") (1 3 4=1 "Weekly"), gen(Week_format)
	recode condition (3=1 "Precise Week") (4=0 "Imprecise Week") (1 2 5=.), gen(Precise_week)
 					   

***Recoding the DV***	  
	gen actual_week_w=.
	recode actual_week_w (.=4135)if q2==1
	replace actual_week_w=q7 if q2==2 | q2==3
	gen actual_week_m=(actual_week_w*52/12)

	gen actual_month_m=.
	recode actual_month_m (.=17918) if q3==1
	replace actual_month_m=q8 if q3==2 | q3==3  

	gen suggested_week_precise_w=.
	recode suggested_week_precise_w(.=6923)if q4==1
	replace suggested_week_precise_w=q9 if q4==2 | q4==3
	gen suggested_week_precise_m=(suggested_week_precise_w*(52/12))

	gen suggested_week_imprecise_w=.
	recode suggested_week_imprecise_w(.=7000)if q5==1
	replace suggested_week_imprecise_w=q10 if q5==2 | q5==3
	gen suggested_week_imprecise_m=(suggested_week_imprecise_w*(52/12))

	gen suggested_month_m=.
	recode suggested_month_m (.=30000) if q6==1
	replace suggested_month_m=q11 if q6==2 | q6==3  

	gen preferred_month=max(actual_week_m, actual_month_m, suggested_week_precise_m, suggested_week_imprecise_m, suggested_month_m)
	gen preferred_week=(preferred_month*12/52)

	gen reply_to_anchor=max(q2, q3, q4, q5, q6)
	recode reply_to_anchor (1=1) (2 3=0), gen(accepting_anchor)

*Dealing with outliers*
	*Trimmed mean (main approach)*
	sum preferred_month, detail
	tab condition, nol
	gen preferred_month_trim5=preferred_month
	foreach x in 1 2 3 4 {
	sum preferred_month_trim5 if condition==`x', d 
	replace preferred_month_trim5=. if condition==`x' & preferred_month_trim5 > r(p95)
	replace preferred_month_trim5=. if condition==`x' & preferred_month_trim5 < r(p5)
	}

	*
	gen preferred_week_trim5=(preferred_month_trim5*12/52)

	
*calculating mean absolute adjustment from anchor*
	gen adjustment=.
	replace adjustment=0-(4135-preferred_week_trim5) if condition==1
	replace adjustment=0-(4135-preferred_week_trim5) if condition==2
	replace adjustment=0-(6923-preferred_week_trim5) if condition==3
	replace adjustment=0-(7000-preferred_week_trim5) if condition==4
	replace adjustment=0-(6923-preferred_week_trim5) if condition==5
	gen adjustment_absolute=abs(adjustment)

*Conflicting answers*
	gen conflict_answer=0
	recode conflict_answer (0=1) if condition==1 & reply_to_anchor==2 & preferred_month>17918
	recode conflict_answer (0=1) if condition==1 & reply_to_anchor==3 & preferred_month<17918
	recode conflict_answer (0=1) if condition==2 & reply_to_anchor==2 & preferred_month>17918
	recode conflict_answer (0=1) if condition==2 & reply_to_anchor==3 & preferred_month<17918
	recode conflict_answer (0=1) if condition==3 & reply_to_anchor==2 & preferred_month>30000
	recode conflict_answer (0=1) if condition==3 & reply_to_anchor==3 & preferred_month<30000
	recode conflict_answer (0=1) if condition==4 & reply_to_anchor==2 & preferred_month>30000
	recode conflict_answer (0=1) if condition==4 & reply_to_anchor==3 & preferred_month<30000
	recode conflict_answer (0=1) if condition==5 & reply_to_anchor==2 & preferred_month>30000
	recode conflict_answer (0=1) if condition==5 & reply_to_anchor==3 & preferred_month<30000
	drop if RecordNo==1130 //This respondent withdrew the answers in the comment section


*Response latencies*
	gen time_on_anchor=max(page_q2_timing, page_q3_timing, page_q4_timing, page_q5_timing, page_q6_timing)
	gen time_on_number=max(page_q7_timing, page_q8_timing, page_q9_timing, page_q10_timing, page_q11_timing)
	gen time_on_anchor_log=log(time_on_anchor)
	gen time_on_number_log=log(time_on_number)
	egen time_on_anchor_rank=rank(time_on_anchor)
	egen time_on_number_rank=rank(time_on_number)


*Completion*
	gen completed=0
	recode completed (0=1) if preferred_month!=.

**********************************************************************************************************************************************************
***Analyses***********************************************************************************************************************************************
**********************************************************************************************************************************************************

***Descriptives of respondents

	**completition**
	tab completed, missing // 1079 started the survey, and 1058 respondents answered all questions for this experiment, 
	di (1079-1058)/1079 // drop-off just 1,9 percent
	tab completed condition if q1!=., exact chi // drop off did not differ significantly among conditions
	**demographics**
	tab female if completed!=0
	sum age if completed!=0
	tab Somecollege if completed!=0
	tab LR if completed!=0, missing
	sum LR_impute if completed!=0
	

	
***Do left and right disagree on the proper sixe of benefits?***
	pwcorr preferred_week_trim5  LR_impute, sig obs
	reg preferred_week_trim5  i.LR_impute
	margins, at(LR_impute=(0(1)10)) post //clear relationship between LR and attitude on benefits. 
	eststo LR_main
	coefplot(LR_main) ///
			, vert legend(rows(1) position(12)) msymbol(circle) msize(large)	///
			title(" ") ytitle("Preferred size of benefits (DKK/week)", size(medium)) xtitle("") 				///
			coeflabels(	1._at="0" ///
						2._at="1" ///
						3._at="2" ///
						4._at="3" ///
						5._at="4" ///
						6._at="5" ///
						7._at="6" ///
						8._at="7" ///
						9._at="8" ///
						10._at="9" ///
						11._at="10" ) ///
						legend(off) msize(large) xtitle("Left-Right Position", size(medium)) level(95) ///
						 xlabel(, labsize(medium) labcolor(black)) ylabel(, labsize(medium) labcolor(black)) ///
						note(" " "Note: Values are trimmed means with 95% C.I. (n=1,012).", size(medium)) title("") ///
						ysize(3) xsize(6)
						graph save ex1_LR_main, replace
						graph export ex1_LR_main.tif, replace //This is Figure 1 in the paper
	
	
	*Two alternative LR-measures yields essentially same result*
	pwcorr preferred_week_trim5  LR_total, sig obs
	reg preferred_week_trim5  LR_total
	reg preferred_week_trim5  i.LR_total_unstd
	margins, at(LR_total_unstd=(1(1)9)) 
	marginsplot
	graph export LR_total.emf, replace
	
	pwcorr preferred_week_trim5  LR, sig obs
	reg preferred_week_trim5  LR
	reg preferred_week_trim5  i.LR_unstd
	margins, at(LR_unstd=(0(1)10)) 
	marginsplot //
	graph export LR.emf, replace



***Do anchor, unit and precision affect stated preferences? 
reg preferred_week_trim5  i.condition, robust
	eststo preferred_week
	margins, at(condition=(1 2 3 4 5)) pwcompare(effects) post //Yes, with one exception, all groups differ significantly from each other
	eststo effect_total
	
	estimates restore preferred_week
	margins, at(condition=(1 2 3 4 5)) post
	eststo effect_main
	coefplot(effect_main) ///
			, vert legend(rows(1) position(12)) msymbol(circle)	msize(large) ///
			title(" ") ytitle("Preferred size of benefits (DKK/week)", size(medium)) xtitle("") 				///
			coeflabels(	1._at=`" "Cond. 1:" "4,135/week" "(Precise)   " "'  ///
						2._at=`" "Cond. 2:  " "17,918/month" "(Precise)     " "'  ///
						3._at= `" "Cond. 3:" "6,923/week" "(Precise)    " "' ///
						4._at= `" "Cond. 4:" "7,000/week" "(Rounded)   " "' ///
						5._at=  `" "Cond. 5:  " "30,000/month" "(Rounded)    " "'  , )	///
						groups(1._at 2._at="Low anchor conditions" 3._at 5._at="High anchor conditions", labsize(medlarge) labcolor(black) ) ///
						legend(off) level(95) ///
						xlabel(, labsize(medsmall) labcolor(black)) ylabel(, labsize(medium) labcolor(black)) ///
						note(" " "Note: Values are trimmed means with 95% C.I. (n=1,012).", size(medium)) title("") ///
						ysize(3) xsize(6)
						graph save ex1_all_conditions, replace
						graph export ex1_all_conditions.tif, replace //This is Figure 2 in the paper
	
					
***How often do respondents accept the anchor? 
	tab accepting_anchor condition if preferred_week_trim5!=., col freq //Large share of respondents accept anchor, but large differences between conditions
	logit accepting_anchor i.condition if preferred_week_trim5!=.
	margins, over(condition) cformat(%9.2f)
	margins, over(condition) pwcompare(effects) //These are the numbers used for Table 1 in the paper


***Are differences attributable to difference in accept of anchor (demand effect)***
	tab accepting_anchor High_anchor, col nofreq //respondents accept low anchor more often than high anchor
	reg preferred_week_trim5 i.condition if accepting_anchor==0, robust //excluding respondents that accept the anchor
	margins, at(condition=(1 2 3 4 5)) pwcompare(effects) // differences are much smaller , but excluding "accepters" results in post treatment bias
	margins, at(condition=(1 2 3 4 5))
	
	pwcorr LR_impute accepting_anchor if High_anchor==0, sig obs //Not a blind demand effect: acceptance of low anchor correlated with LR-position
	pwcorr LR_impute accepting_anchor if High_anchor==1, sig obs //Not a blind demand effect: acceptance of high anchor correlated with LR-position
	
	reg preferred_week_trim5  c.LR_impute##i.condition if accepting_anchor==0, robust			
	margins, at(condition=(1 2 3 4 5)) pwcompare(effects) // differences generally become a bit larger again when controlling for LR, but this does not solve issue of post treatment bias
	margins, at(condition=(1 2 3 4 5)) 


***Regression used for analyses of LR_interactions
reg preferred_week_trim5  i.LCR##i.condition, robust
	eststo pref_week_int_c3
	
	estimates restore pref_week_int_c3
	margins, at(condition=(1 2 3 4 5) LCR=0) pwcompare(effects)
	//
	margins, at(condition=(1 2 3 4 5) LCR=0)   post
	eststo effect_left
	
	estimates restore pref_week_int_c3
	margins, at(condition=(1 2 3 4 5) LCR=1) pwcompare(effects)
	//
	margins, at(condition=(1 2 3 4 5) LCR=1)   post
	eststo effect_center
		
	estimates restore pref_week_int_c3
	margins, at(condition=(1 2 3 4 5) LCR=2) pwcompare(effects)
	//
	margins, at(condition=(1 2 3 4 5) LCR=2)   post
	eststo effect_right

	*graph
	coefplot(effect_left, label("Left") msymbol(circle) msize(large) mfcolor(gs15) mlcolor(black) ciopts(lcolor(black)) offset(-.1)) 	///
			(effect_center, label("Center") msymbol(circle) msize(large) mfcolor(gs10) mlcolor(black) ciopts(lcolor(black)) offset(-0)) ///
			(effect_right, label("Right") msymbol(circle) msize(large) mfcolor(gs15) mlcolor(black) ciopts(lcolor(black)) offset(.1)) ///
				, vert legend(rows(1) subtitle("Position on Left-Right Scale:", position(9)) position(12) region(lcolor(gs10) lwidth(vthin)) size(medium)) 	///
				title(" ") ytitle("Preferred size of benefits (DKK/week)", size(medium)) xtitle("") 				///
				coeflabels(	1._at=`" "Cond. 1:" "4.135/week" "(Precise)   " "'  ///
							2._at=`" "Cond. 2:  " "17.918/month" "(Precise)     " "'  ///
							3._at= `" "Cond. 3:" "6,923/week" "(Precise)    " "' ///
							4._at= `" "Cond. 4:" "7,000/week" "(Rounded)   " "' ///
							5._at=  `" "Cond. 5:  " "30,000/month" "(Rounded)    " "'  , )	///
							groups(1._at 2._at="Low anchor conditions" 3._at 5._at="High anchor conditions", labsize(medium) labcolor(black)) ///
							msize(medlarge) level(95)  ///
							xlabel(, labsize(medsmall) labcolor(black)) ylabel(, labsize(medium) labcolor(black)) ///
							note(" " "Note: Values are trimmed means with 95% C.I. (n=1,012).", size(medium)) title("") ///
							ysize(3) xsize(6)
							graph save ex1_LR_all_conditions, replace
							graph export ex1_LR_all_conditions.tif, replace //This is Figure 3 in the paper
	
	

**********************************************************************************************************************************************************
***Power calculation for study 2 *************************************************************************************************************************
**********************************************************************************************************************************************************

***Difference betweeen precise and imprecise in study 1:
	sum preferred_week_trim5 if condition==3
	sum preferred_week_trim5 if condition==4
	di  (6116.462-5705.472)/2 //half the difference

***Power calculation based on conservative estimate of half the effect found in study 1:
	power twomeans 6116.462, sd(1344.779) diff(205.495) alpha(.05) power(.8) // 674 subjects per group, almost 2,000 for all three groups


**********************************************************************************************************************************************************
***Robustness*********************************************************************************************************************************************
**********************************************************************************************************************************************************


//What happens if we exclude respondents with conflicting answers?
reg preferred_week_trim5 i.condition if conflict_answer!=1, robust  //
	margins, over(condition) pwcompare(effects) //results remain unchanged
	margins, over(condition)


//What happens if we exclude respondents adjusting upwards in conditions 3 and 4?
reg preferred_week_trim5 i.condition if adjustment<1 & condition<5 & condition>2, robust  //
	margins, over(condition) pwcompare(effects) //Conditions 3 and 4 still differ significantly from each other
	margins, over(condition)



//what happens if we use median instead of trimmed means?

	*LR-relationsship*
	qreg preferred_week  LR_impute
	qreg preferred_week  i.LR_impute
	margins, at(LR_impute=(0(1)10)) post //Pattern is essentiall similar to analysis with trimmed means
	eststo ex1_LR_median
	coefplot(ex1_LR_median)  ///
			, vert legend(rows(1) position(12)) msymbol(circle)	///
			title(" ") ytitle("Preferred size of benefits (DKK/week)", size(medium)) xtitle("") 				///
			coeflabels(	1._at="0" ///
						2._at="1" ///
						3._at="2" ///
						4._at="3" ///
						5._at="4" ///
						6._at="5" ///
						7._at="6" ///
						8._at="7" ///
						9._at="8" ///
						10._at="9" ///
						11._at="10" ) ///
						legend(off) msize(large) xtitle("Left-Right Position", size(medium)) level(95) ///
						title("Quantile Regression", size(medium) box fcolor(none) lcolor(none) margin(small)) ///
						xlabel(, labsize(medium) labcolor(black)) ylabel(, labsize(medium) labcolor(black)) ///
						ysize(3) xsize(6)
						graph save ex1_LR_median, replace
						graph export ex1_LR_median.tif, replace //This is used for Figure A1 in appendix
	
	
		
	*Experimental treatment effects*	
	qreg preferred_week i.condition //
	margins, at(condition=(1 2 3 4 5)) pwcompare(effects) //unlike analysis with trimmed means, difference between condtion 4 and 5 is insignificant. All other results substantively similar
	margins, over(condition)
	marginsplot, recast(scatter) xlabel(, labsize(small) angle(forty_five)) horizontal xlabel(3000(1000)7000) xtitle("")
	
	
	qreg preferred_week_trim5  i.LCR##i.condition
	margins, at(condition=(1 2 3 4 5) LCR=0) pwcompare(effects)
	margins, at(condition=(1 2 3 4 5) LCR=1) pwcompare(effects)
	margins, at(condition=(1 2 3 4 5) LCR=2) pwcompare(effects)
		
	
//what happens if we use robust regression instead of trimmed means?
	rreg preferred_week  LR_impute
	rreg preferred_week  i.LR_impute
	margins, at(LR_impute=(0(1)10)) post //Pattern is essentiall similar to analysis with trimmed means
	eststo ex1_LR_robust
	coefplot(ex1_LR_robust) ///
			, vert legend(rows(1) position(12)) msymbol(circle)	///
			title(" ") ytitle("Preferred size of benefits (DKK/week)", size(medium)) xtitle("") 				///
			coeflabels(	1._at="0" ///
						2._at="1" ///
						3._at="2" ///
						4._at="3" ///
						5._at="4" ///
						6._at="5" ///
						7._at="6" ///
						8._at="7" ///
						9._at="8" ///
						10._at="9" ///
						11._at="10" ) ///
						legend(off) msize(large) xtitle("Left-Right Position", size(medium)) level(95) ///
						title("Robust Regression", size(medium) box fcolor(none) lcolor(none) margin(small)) ///
						xlabel(, labsize(medsmall) labcolor(black)) ylabel(, labsize(medium) labcolor(black)) ///
						ysize(3) xsize(6)
						graph save ex1_LR_robust, replace
						graph export ex1_LR_robust.tif, replace //This is used for Figure A1 in appendix
			
	
	*Experimental treatment effects*	
	rreg preferred_week i.condition //
	margins, over(condition) pwcompare(effects) //Pattern is essentiall similar to analysis with trimmed means
	margins, over(condition)
	marginsplot, recast(scatter) xlabel(, labsize(small) angle(forty_five)) horizontal xlabel(3000(1000)7000) xtitle("")

	rreg preferred_week_trim5  i.LCR##i.condition
	margins, at(condition=(1 2 3 4 5) LCR=0) pwcompare(effects)
	margins, at(condition=(1 2 3 4 5) LCR=1) pwcompare(effects)
	margins, at(condition=(1 2 3 4 5) LCR=2) pwcompare(effects)


//combined graph
graph combine "ex1_LR_median" "ex1_LR_robust", ycommon note(" " "Note: Estimates with 95% C.I. (n=1,058 for both graphs).") 
graph export ex1_LR_alternatives.tif, replace //This is figure A1 in the appendix

erase ex1_LR_main.gph
erase ex1_all_conditions.gph
erase ex1_LR_all_conditions.gph
erase ex1_LR_median.gph
erase ex1_LR_robust.gph
erase ex1_LR_median.tif
erase ex1_LR_robust.tif

*************************
***THE END***************
*************************
